<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发货通知单编码" prop="consignmentId" label-width="110px">
        <el-input v-model="queryParams.consignmentId" placeholder="请输入发货通知单编码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="出库日期" prop="outboundDate">
        <el-date-picker clearable v-model="queryParams.outboundDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择出库日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户型号" prop="customerModel">
        <el-select v-model="queryParams.customerModel" placeholder="请选择审核状态" clearable>
          <el-option v-for="dict in dict.type.customer_model" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="销售订单号" prop="clientOrderNumber" label-width="85px">
        <el-input v-model="queryParams.clientOrderNumber" placeholder="请输入销售订单号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="颜色" prop="color">
        <el-select v-model="queryParams.color" filterable placeholder="请选择颜色" clearable>
          <el-option v-for="dict in dict.type.colour" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:outbound:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:outbound:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="audit" plain size="mini" :disabled="multiple" @click="handleAudit"
          v-hasPermi="['system:outbound:audit']">审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="audit" plain size="mini" :disabled="multiple" @click="UnhandleAudit"
          v-hasPermi="['system:outbound:unaudit']">反审核</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:outbound:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:outbound:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="outboundList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="发货通知单编码" align="center" prop="consignmentId" />
      <el-table-column label="出库类型" align="center" prop="types">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.outbound_types" :value="scope.row.types" />
        </template>
      </el-table-column>
      <el-table-column label="销售订单号" align="center" prop="clientOrderNumber" />
      <el-table-column label="发货通知单日期" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出库日期" align="center" prop="outboundDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.outboundDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户" align="center" prop="clientName" />
      <el-table-column label="产品编码" align="center" prop="productId" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="颜色" align="center" prop="color">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.colour" :value="scope.row.colour" />
        </template>
      </el-table-column>
      <el-table-column label="规格型号" align="center" prop="specifications">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.specification_and_model" :value="scope.row.specifications" />
        </template>
      </el-table-column>
      <el-table-column label="客户型号" align="center" prop="customerModel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.customer_model" :value="scope.row.customerModel" />
        </template>
      </el-table-column>
      <el-table-column label="出货仓库" align="center" prop="warehouse" />
      <el-table-column label="应出库数量" align="center" prop="weekdaying" />
      <el-table-column label="已出库数量" align="center" prop="quantityShipped" />
      <el-table-column label="本次出库数量" align="center" prop="weekdayed" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="审核状态" align="center" prop="audit">
        <template #default="{ row }">
          <el-tag v-if="row.audit == 0" type="danger">未审核</el-tag>
          <el-tag v-if="row.audit == 2" type="success">已审核</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:outbound:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 添加或修改出库单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="发货通知单编码" prop="consignmentId" v-show="consignmentId == null">
          <el-input v-model="form.consignmentId" placeholder="请输入发货通知单编码" />
        </el-form-item>
        <el-form-item label="出库日期" prop="outboundDate">
          <el-date-picker clearable v-model="form.outboundDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择单据日期">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="颜色" prop="colour">
          <el-select v-model="form.colour" placeholder="请选择颜色">
            <el-option v-for="dict in dict.type.colour" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item> -->

        <el-form-item label="柜号" prop="containerNumber" @keyup.enter.native="nextGetFocus">
          <el-input v-model="form.containerNumber" placeholder="请输入柜号" />
        </el-form-item>
        <el-form-item label="本次出库数量" prop="weekdayed">
          <el-input v-model="form.weekdayed" disabled />
        </el-form-item>
        <el-form-item label="车辆ID" prop="bicycleIds">
          <el-input type="textarea" :rows="2" placeholder="请输入车辆ID" v-model="form.bicycleIds"
            @keydown.enter.native="keydownId">
          </el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="Staging">暂存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOutbound, getOutbound, delOutbound, addOutbound, updateOutbound, updateAudit, updateUnAudit } from "@/api/outbound/outbound/outbound";

export default {
  name: "Outbound",
  dicts: ['colour', 'outbound_types', "customer_model", "specification_and_model"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出库单表格数据
      outboundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        consignmentId: null
      },

      //接收参数
      consignmentId: null,
      // 表单参数
      form: {

      },
      // 表单校验
      rules: {
        // consignmentId: [{ required: true, message: '发货通知单编码', trigger: 'blur' }],
        containerNumber: [{ required: true, message: '柜号不能为空', trigger: 'blur' }],
        bicycleIds: [{ required: true, message: '车辆ID不能为空', trigger: 'blur' }]
      }
    };
  },
  mounted() {
    this.queryParams.consignmentId = this.$route.query.consignmentId
    console.log(this.$route.query.consignmentId);
    this.getList();
  },
  created() {
    this.consignmentId = this.$route.query.consignmentId
    this.getList();
  },
  methods: {
    //添加多个车辆ID回车事件
    keydownId() {
      this.form.weekdayed++
      this.form.bicycleIds = this.form.bicycleIds + ";"
    },
    //下一个输入框获取焦点
    nextGetFocus() {
      this.$nextTick(() => {
        let inputTail = this.form.bicycleIds.length - 1
        this.$refs.refInput[inputTail].focus()
      })
    },
    /** 查询出库单列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams);
      listOutbound(this.queryParams).then(response => {
        this.outboundList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //新增车辆ID
    // addDomain(item) {

    //   this.form.bicycleIds.push({
    //     value: this.form.bicycleIds.value,
    //     key: Date.now()
    //   });
    //   this.$nextTick(() => {
    //     let inputTail = this.form.bicycleIds.length - 1
    //     this.$refs.refInput[inputTail].focus()
    //   })
    // },
    //删除车辆ID
    removeDomain(item) {
      var index = this.form.bicycleIds.indexOf(item)
      console.log(item);
      if (index !== -1) {
        this.form.bicycleIds.splice(index, 1)
      }
    },
    // 表单重置
    reset() {
      this.form = {
        consignmentId: null,
        weekdayed: null,
        containerNumber: null,
        remark: null,
        delFlag: null,
        color: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.containerNumber)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    //暂存
    Staging() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.consignmentId != null) {
            updateOutbound(this.form).then(response => {
              this.$modal.msgSuccess("暂存成功");
              this.open = true;
            });
          } else {
            this.form.consignmentId = this.consignmentId
            addOutbound(this.form).then(response => {
              this.$modal.msgSuccess("暂存成功");
              this.open = true;
            });
          }
        }
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.outboundDate = new Date()
      this.open = true;
      this.form.weekdayed = 0;
      this.title = "添加出库单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const containerNumber = row.containerNumber || this.ids
      getOutbound(containerNumber).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改出库单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.consignmentId != null) {
            updateOutbound(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.consignmentId = this.consignmentId
            console.log(this.form);
            addOutbound(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 审核操作 */
    handleAudit(row) {
      const containerNumbers = row.containerNumber || this.ids;
      updateAudit(containerNumbers);
      this.getList();
      this.$modal.msgSuccess("审核成功");

    },
    UnhandleAudit(row) {
      const containerNumbers = row.containerNumber || this.ids;
      updateUnAudit(containerNumbers);
      this.getList();
      this.$modal.msgSuccess("反审核成功");

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const containerNumbers = row.consignmentId || this.ids;
      this.$modal.confirm('是否确认删除出库单编号为"' + containerNumber + '"的数据项？').then(function () {
        return delOutbound(containerNumbers);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/outbound/export', {
        ...this.queryParams
      }, `outbound_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.input {
  width: 80% !important
}
</style>
